// 挂载时引入评分触点组件  App.vue中不能有相关标签的注释代码，否则逻辑判断会有误
export default function RankContactPlugin() {
  return {
    name: "RankContactPlugin",
    async transform(code, id) {
      if (id.endsWith("App.vue")) {
        // 在</template>标签之前插入组件
        const templateIndex = code.indexOf("</template>");
        code =
          code.slice(0, templateIndex) +
          "<RankContact/>" +
          code.slice(templateIndex);
        // 对于vue3写法
        const scriptIndex = code.indexOf("<script setup lang='ts'>");
        const scriptIndex1 = code.indexOf("<script setup>");
        const useTs = code.indexOf("lang='ts'");
        // 没有使用ts的 在<script setup>标签后插入引入组件代码
        if (scriptIndex1 !== -1 && useTs === -1) {
          const insertIdx = scriptIndex1 + "<script setup>".length;
          code =
            code.slice(0, insertIdx) +
            "\nimport RankContact from '@/components/RankContact.vue';" +
            code.slice(insertIdx);
        }
        // 使用了ts写法 在<script setup>标签后插入引入组件代码
        if (scriptIndex1 !== -1 && useTs !== -1) {
          console.log("useTs");
          if (scriptIndex !== -1) {
            const insertIdx = scriptIndex + "<script setup lang='ts'>".length;
            code =
              code.slice(0, insertIdx) +
              "\nimport RankContact from '@/components/RankContact.vue';" +
              code.slice(insertIdx);
          }
        }
        // 对于vue2写法 在<script>标签后插入引入组件代码
        const scriptIndex2 = code.indexOf("<script>");
        if (scriptIndex2 !== -1) {
          const insertIdx = scriptIndex2 + "<script>".length;
          code =
            code.slice(0, insertIdx) +
            "\nimport RankContact from '@/components/RankContact.vue'" +
            code.slice(insertIdx);
        }
        // 对于vue2写法 在script中注册vue文件
        if (code.indexOf("components: {") !== -1) {
          const scriptIndex3 = code.indexOf("components: {");
          if (scriptIndex3 !== -1) {
            const insertIdx = scriptIndex3 + "components: {".length;
            code =
              code.slice(0, insertIdx) +
              "\nRankContact,\n" +
              code.slice(insertIdx);
          }
        } else {
          const scriptIndex4 = code.indexOf("export default {");
          if (scriptIndex4 !== -1) {
            const insertIdx = scriptIndex4 + "export default {".length;
            code =
              code.slice(0, insertIdx) +
              "\ncomponents:{ RankContact },\n" +
              code.slice(insertIdx);
          }
        }
        // 对于没有script的
        // if (scriptIndex === -1 && scriptIndex1 === -1 && scriptIndex2 === -1) {
        //   console.log(">>>> Hello >>>>");
        //   const templateIndex = code.indexOf("</template>");
        //   const insertIdx = templateIndex + "</template>".length;
        //   // code = code.slice(0, insertIdx) + "\n<script setup>\nimport RankContact from '@/components/RankContact.vue';\n</script>" + code.slice(insertIdx);
        // }
        // 其他特殊写法适配...
      }
      return code;
    },
  };
}
